package cn.itcast.goods.admin.category.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.jmx.snmp.daemon.CommunicationException;

import cn.itcast.commons.CommonUtils;
import cn.itcast.goods.category.doamin.Category;
import cn.itcast.goods.category.service.CategoryService;
import cn.itcast.servlet.BaseServlet;

public class AdminCategoryServlet extends BaseServlet {

	private CategoryService categoryService = new CategoryService();
    /**
     * 获取所有的分类
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
	public String findAll(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setAttribute("cList",categoryService.findAll());
	    return "/adminjsps/admin/category/list.jsp";
	}
    /**
     * 添加一级分类
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
	public String addOneLevel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Category category=CommonUtils.toBean(request.getParameterMap(), Category.class);
		category.setCid(CommonUtils.uuid());
        categoryService.add(category);
		return findAll(request, response);
	}
	
	/**
	 * 添加二级分类,第一步
	 * 传递以及的cid
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addTwoLevelPre(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pid = request.getParameter("pid");
		request.setAttribute("pid", pid);
		request.setAttribute("parents", categoryService.findByParents());
		return "/adminjsps/admin/category/add2.jsp";
	}
	
	/**
	 * 添加二级分类，第二步
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String addTwoLevel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Category child = CommonUtils.toBean(request.getParameterMap(), Category.class);
		child.setCid(CommonUtils.uuid());
		Category parent = new Category();
		parent.setCid(request.getParameter("pid"));
		child.setParent(parent);
		child.setPid(request.getParameter("pid"));
		categoryService.add(child);
		return findAll(request, response);
	}
	
	/**
	 * 编辑一级分类，第一步
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editOneLevelPre(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cid = request.getParameter("cid");
		request.setAttribute("category", categoryService.load(cid));
		return "f:/adminjsps/admin/category/edit.jsp";
	}
	/**
	 * 编辑一级分类,第二步
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editOneLevel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Category category = CommonUtils.toBean(request.getParameterMap(), Category.class);
		categoryService.edit(category);
		return findAll(request, response);
	}
	/**
	 * 编辑二级分类，第一步
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editSecondLevelPre(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cid = request.getParameter("cid");
		request.setAttribute("child", categoryService.load(cid));
		request.setAttribute("parents", categoryService.findByParents());
		return "/adminjsps/admin/category/edit2.jsp";
	}
	
	/**
	 * 编辑二级分类，第二步
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String editSecondLevel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Category child = CommonUtils.toBean(request.getParameterMap(), Category.class);
		Category parent = new Category();
		String pid = request.getParameter("pid");
		child.setParent(parent);
		categoryService.edit(child);
		return findAll(request, response);
	}
	
	/**
	 * 刪除一級分類
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteOneLevel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cid = request.getParameter("cid");
		
		try {
			categoryService.deleteOneLevel(cid);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			return "/adminjsps/msg.jsp";
		}
		return findAll(request, response);
				
	}
	/**
	 * 删除二级分类
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String deleteTwoLevel(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String cid = request.getParameter("cid");
		
		try {
			categoryService.deleteTwoLevel(cid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			return "/adminjsps/msg.jsp";
		}
		return findAll(request, response);
	}
	
	/**
	 * 	
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAllForBook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setAttribute("parents", categoryService.findAll());
		return "/adminjsps/admin/book/left.jsp";
	}
	
	/**
	 * 异步请求，查询指定以及分类下面的所有二级
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String childrenForAjax(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pid = request.getParameter("pid");
		List<Category> children = categoryService.findChildren(pid);
		String json = toJsonArray(children);
		response.getWriter().print(json);
		return null;
	}
	
	/**
	 * 把数据转换成json
	 * @param children
	 * @return
	 */
	private String toJsonArray(List<Category> categoryList) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder("[");
		int size = categoryList.size();
		for(int i=0;i<size;i++)
		{
			sb.append(toJson(categoryList.get(i)));
			if(i < size-1)
			{
				sb.append(",");
			}
		}
		sb.append("]");
		return sb.toString();
	}
	private Object toJson(Category category) {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder("{");
		sb.append("\"cid\":").append("\"").append(category.getCid()).append("\",");
		sb.append("\"cname\":").append("\"").append(category.getCname()).append("\"");
		sb.append("}");
		return sb.toString();
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	
	}

}
